package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.Type;
import com.ruoyi.system.domain.vo.TypeLogVO;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.TypeMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TypeLogMapper;
import com.ruoyi.system.domain.TypeLog;
import com.ruoyi.system.service.ITypeLogService;

import javax.annotation.Resource;

/**
 * 采集记录Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-30
 */
@Service
public class TypeLogServiceImpl implements ITypeLogService {
    @Resource
    private TypeLogMapper typeLogMapper;
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private TypeMapper typeMapper;

    /**
     * 查询采集记录
     *
     * @param id 采集记录主键
     * @return 采集记录
     */
    @Override
    public TypeLog selectTypeLogById(Long id) {
        TypeLog typeLog = typeLogMapper.selectTypeLogById(id);
        format(typeLog);
        return typeLog;
    }

    /**
     * 查询采集记录列表
     *
     * @param typeLog 采集记录
     * @return 采集记录
     */
    @Override
    public List<TypeLog> selectTypeLogList(TypeLog typeLog) {
        List<TypeLog> typeLogs = typeLogMapper.selectTypeLogList(typeLog);
        typeLogs.forEach(this::format);
        return typeLogs;
    }

    private void format(TypeLog typeLog) {
        if (typeLog.getUserId() != null) {
            SysUser sysUser = sysUserMapper.selectUserById(typeLog.getUserId());
            if (sysUser != null) {
                typeLog.setUserName(sysUser.getUserName());
            }
        }
        if (typeLog.getTypeId() != null) {
            Type type = typeMapper.selectTypeById(typeLog.getTypeId());
            if (type != null) {
                typeLog.setTypeName(type.getName());
            }
        }
    }

    /**
     * 新增采集记录
     *
     * @param typeLog 采集记录
     * @return 结果
     */
    @Override
    public int insertTypeLog(TypeLog typeLog) {
        typeLog.setCreateTime(DateUtils.getNowDate());
        return typeLogMapper.insertTypeLog(typeLog);
    }

    /**
     * 修改采集记录
     *
     * @param typeLog 采集记录
     * @return 结果
     */
    @Override
    public int updateTypeLog(TypeLog typeLog) {
        typeLog.setUpdateTime(DateUtils.getNowDate());
        return typeLogMapper.updateTypeLog(typeLog);
    }

    /**
     * 批量删除采集记录
     *
     * @param ids 需要删除的采集记录主键
     * @return 结果
     */
    @Override
    public int deleteTypeLogByIds(Long[] ids) {
        return typeLogMapper.deleteTypeLogByIds(ids);
    }

    /**
     * 删除采集记录信息
     *
     * @param id 采集记录主键
     * @return 结果
     */
    @Override
    public int deleteTypeLogById(Long id) {
        return typeLogMapper.deleteTypeLogById(id);
    }
}
